package com.tiantian.quanminxiu.presenter;

import android.util.Log;

import com.tiantian.quanminxiu.api.AnchorService;
import com.tiantian.quanminxiu.utils.ApplicationBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.MainView;
import com.shenglian.utils.utils.CommonUtils;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by admin on 2016/8/11.
 */
public class MainPresent implements Presenter<MainView> {

    private MainView mainView;

    @Override
    public void attachView(MainView view) {
        this.mainView = view;
    }

    @Override
    public void detachView() {
        mainView = null;
    }


    public void initMain(int total,int iDisplayStart,int iDisplayLength){
        mainView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(mainView.getContext());
        AnchorService service = app.getAnchorService();
        service.user_useranchor_page(UserData.Current.mid,UserData.Current.sign,Constants.APP_TYPE,"user.user-anchor.page",
                true,true,total,0,iDisplayStart,iDisplayLength,"Heat")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    mainView.Loading_Close();
                })
                .subscribe(str -> {
                    Log.i("dd","--dd:"+str.toString());
                    mainView.getData(str.optJSONArray("aaData"),iDisplayStart+iDisplayLength);
                }, throwable -> {
                    CommonUtils.showToask(mainView.getContext(), throwable.getMessage());
                });

    }

}
